import React, {Component} from 'react';
import {Relax} from 'iflux2';
import {Map, List} from 'immutable';

import {Row, Col, InputNumber} from 'antd';

import noop from 'util/noop';
import calculator from 'util/calculator';


@Relax
export default class OrderInfo extends Component {
    static defaultProps = {
        order: Map(),
        itemOrders: List(),
        taxRate: 0,
        changeOrderField: noop
    }


    render() {
        const {order} = this.props;

        return (
            <Row className="order-info-former">
                <Col span={9} offset={15}>
                    <Row className="pushb t-grayer">
                        <Col span={12}><label className="lh20">商品总价：</label></Col>
                        <Col span={12}><div className="t-right">&yen;{this.interception((this.calculateinitItemPriceTotal() || 0), 2).toFixed(2)}</div></Col>
                    </Row>
                    <Row className="pushb t-grayer">
                        <Col span={12}><label className="lh20">物流运费：</label></Col>
                        <Col span={12}>
                            <div className="t-right">
                                <span className="t-red">&yen;</span>
                                <InputNumber value={order.get('logisticsFee')}
                                             className="w80 ml3 t-right"
                                             min={0}
                                             max={9999999.99}
                                             onChange={(value)=>this.changeOrderField(value, 'logisticsFee')}/>
                            </div>
                        </Col>
                    </Row>
                    <Row className="pushb t-grayer">
                        <Col span={12}><label className="lh20">发票费用：</label></Col>
                        <Col span={12}>
                            <div className="t-right">&yen;{this.interception((this.calculateInvoiceFee() || 0), 2).toFixed(2)}</div>
                        </Col>
                    </Row>
                    <Row className="pushb t-grayer">
                        <Col span={12}><label className="lh20">店铺优惠：</label></Col>
                        <Col span={12}><div className="t-right">-&yen;{this.interception((this.calculatePrivilege() || 0), 2).toFixed(2)}</div></Col>
                    </Row>
                    <Row className="pushb font-14">
                        <Col span={12}><label className="lh20">订单总价：</label></Col>
                        <Col span={12}><div className="t-right">&yen;{this.interception((this.calculateTradeTotal() || 0), 2).toFixed(2)}</div></Col>
                    </Row>
                </Col>
            </Row>
        )
    }

    /**
     * 发票费用
     * @returns {number}
     */
    calculateInvoiceFee = ()=>{
        const {taxRate, order} = this.props;

        return calculator.floatMul(calculator.floatSub(this.calculateItemTotal(), order.get('offsetMoney'))
                , taxRate) / 100;
    }


    /**
     * 优惠金额
     */
    calculatePrivilege = ()=>{
        const {order} = this.props;

        return calculator.floatSub(this.calculateinitItemPriceTotal(), this.calculateItemTotal())
    }


    /**
     * 商品成交总价
     * @returns {*}
     */
    calculateItemTotal = ()=>{
        const {itemOrders} = this.props;

        return itemOrders.reduce((total, item)=>{
            return calculator.floatAdd(total, calculator.floatMul(item.get('pervalue'), item.get('itemNumber')));
        }, 0);
    }


    calculateinitItemPriceTotal = ()=>{
        const {itemOrders} = this.props;

        return itemOrders.reduce((total, item)=>{
            return calculator.floatAdd(total
            , calculator.floatMul(item.get('initItemPrice'), item.get('itemNumber')))
        }, 0)
    }


    /**
     * 订单总价
     */
    calculateTradeTotal = ()=>{
        const {order} = this.props;
        return calculator.floatAdd(calculator.floatAdd(this.calculateItemTotal(), this.calculateInvoiceFee())
            , order.get('logisticsFee'));
    }




    /**
     * 修改订单某项费用
     * @param value
     * @param field
     */
    changeOrderField(value, field) {
        if (value == undefined) {
            return ;
        }
        this.props.changeOrderField(Math.floor(value * 100) / 100, field);
    }


    interception = (origin, bits)=> {
        let m = Math.pow(10, bits);

        return Math.floor(origin * m) / m;
    }
}

